/**
 * Created by wangfan on 2017/6/9.
 */
function registFileInput(inputSelector,url,successSelector,errorSelector,inputField,formSelector){

    $(inputSelector).fileinput({
        uploadUrl: url, // server upload action
        uploadAsync: true,
        showPreview: false,
        allowedFileExtensions: ["doc", "docx", "wps"],
        language: 'zh',
        maxFileCount: 1,
        elErrorContainer: errorSelector
    }).on("fileuploaded", function (event, data, id, index) {

        var response = data.response;
        var status = response.status;
        var out;
        if (status == 0) {
            var fname = data.files[index].name;
            out = '上传文件  - ' +
                fname + ' 成功.';
            $('#'+inputField).val(response.data);
            $(formSelector).bootstrapValidator('revalidateField', inputField);
            $(successSelector).fadeIn('slow');
            alert("上传成功");
        } else if (status == 1) {
            alert("上传失败");
        } else {
            out = '该文件已经存在，请不要重复上传，或更改名称后上传';
            $('#'+inputField).val(response.data);
            alert("该文件已经存在，请不要重复上传，或更改名称后上传");

        }
        $(successSelector).html(out);
        $(successSelector).fadeIn('slow');


    }).on('fileclear', function(event) {
        $(inputSelector).val("");
        $(successSelector).hide();
        $(formSelector).bootstrapValidator('revalidateField', inputField)

    });


}

$(document).ready(function () {
    $.ajax({
        url: '/commis/api/user/currentUser',
        type: "GET",
        async: false,
        dataType: 'json',
        data: {},
        success: function (response) {
            if (response.status == 0) {
                console.log("用户存在");
                user = response.data;
                userTypeId = user.userTypeId;
                if (userTypeId == 4) {
                    var communityName = user.community.communityName;

                    $("#communityName").val(communityName);

                }
            } else if (response.status == 1) {
                alert("登录用户已经过期，请重新登录");
                window.parent.location.href = "../../login.html";
            }
        }, error: function (msg) {
            alert("登录用户已经过期，请重新登录");
            window.parent.location.href = "../../login.html";
        }
    })


    $('#dataForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {/*输入框不同状态，显示图片的样式*/
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {/*验证*/
            pfTitle: {/*键名username和input name值对应*/
                message: '标题不能为空',
                validators: {
                    notEmpty: {/*非空提示*/
                        message: '标题不能为空'
                    }
                }
            },
            pfTime: {
                message:'时间不能为空',
                validators: {
                    notEmpty: {
                        message: '时间不能为空'
                    },
                    date: {/*长度提示*/
                        format : 'YYYY-MM-DD',
                        message : '时间格式不正确，必须为YYYY-MM-DD格式'
                    }/*最后一个没有逗号*/
                }
            },
            file: {
                message:'文件不能为空',
                validators: {
                    notEmpty: {
                        message: '文件不能为空'
                    }
                }
            }, attachpath1: {
                message:'文件不能为空',
                validators: {
                    notEmpty: {
                        message: '文件不能为空'
                    }
                }
            }, attachpath2: {
                message:'文件不能为空',
                validators: {
                    notEmpty: {
                        message: '文件不能为空'
                    }
                }
            }, attachpath3: {
                message:'文件不能为空',
                validators: {
                    notEmpty: {
                        message: '文件不能为空'
                    }
                }
            }
        }
    });
    $('#pfTime').datetimepicker({
        language: 'zh-CN',
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true,
        todayBtn: true,


    }).on('changeDate show', function(e) {
        $('#dataForm').bootstrapValidator('revalidateField', 'pfTime');
    });;
    registFileInput('#inputfile1','/commis/api/upload/uploadPublicFundFile','.file-success','.file-error','attachpath1','#dataForm','file');
    registFileInput('#inputfile2','/commis/api/upload/uploadPublicFundFile','.file-success2','.file-error2','attachpath2','#dataForm','file');
    registFileInput('#inputfile3','/commis/api/upload/uploadPublicFundFile','.file-success3','.file-error3','attachpath3','#dataForm','file');


    $("#save").on("click", function () {

        //获取表单对象
        var bootstrapValidator = $("#dataForm").data('bootstrapValidator');
        //手动触发验证
        bootstrapValidator.validate();
        if (bootstrapValidator.isValid()) {

            $.ajax({
                type: "POST",
                url:"/commis/api/publicFund/muticreate",
                data:decodeURIComponent($('#dataForm').serialize(),true),
                async: false,
                error: function(request) {
                    alert("Connection error");
                },
                success: function(response) {
                    //接收后台返回的结果
                    if (response.status==0) {
                        confirm("申请成功，是否关闭当前窗口？")
                        window.parent.document.getElementById("tabCloseCurrent").click();
                    } else if (response.status==1) {
                        alert(response.msg);
                    }

                }
            });



            //提交不了
            // $("#submitter").submit();
        }else{
            alert("数据填写不正确，请按照提示信息修改后提交");
        }


    });



})
//设置框架
$(function () {
    $("#page_style").frame({
        float: 'left',
        menu_nav: '.Quick_operation',
        color_btn: '.skin_select',
        Sellerber_menu: '.list_content',
        Sellerber_header: '.Sellerber_header',
    });
});
$("#iframe_box").niceScroll({
    cursorcolor: "#888888",
    cursoropacitymax: 1,
    touchbehavior: false,
    cursorwidth: "5px",
    cursorborder: "0",
    cursorborderradius: "5px"
});
/*********************/
require.config({
    paths: {
        echarts: './js/dist'
    }
});